package com.hk.yajin.function.messages;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.hk.developmentlib.baseben.NewWorkBean;
import com.hk.developmentlib.callback.HttpCallBack;
import com.hk.developmentlib.utills.FastjsonUtils;
import com.hk.yajin.R;
import com.hk.yajin.base.BaseActivity;
import com.hk.yajin.function.members.activity.RechargeGlodActivity;
import com.hk.yajin.manager.UserInfoManager;
import com.hk.yajin.model.MessageType;
import com.hk.yajin.model.MessageTypeChildBean;
import com.hk.yajin.utills.BaseItemBottomDecoration;
import com.hk.yajin.utills.HttpRequestUtils;
import com.hk.yajin.utills.NotificationsUtils;
import com.hk.yajin.utills.url.UrlConstants;
import com.hk.yajin.view.PopupWindows;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class BuyMessageActivity extends BaseActivity implements BuyMessageMultQuickAdapter.OnlickItemClick {
    protected RecyclerView mRecyclerView;
    private List<MultiItemEntity> datas = new ArrayList<>();
    private BuyMessageMultQuickAdapter mQuickAdapter;
    protected SwipeRefreshLayout swipe_container;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_buy_message);
        initView();
        initData();
    }

    private void initView() {
        initToolBar(R.string.buy_message);
        swipe_container = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
        swipe_container.setEnabled(false);
        mQuickAdapter = new BuyMessageMultQuickAdapter(datas, this, this);
        mQuickAdapter.setEnableLoadMore(false);
        mRecyclerView = (RecyclerView) findViewById(R.id.rv_list);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mRecyclerView.setAdapter(mQuickAdapter);
        mRecyclerView.addItemDecoration(new BaseItemBottomDecoration(this, 1));
        mQuickAdapter.expandAll();
    }

    private void initData() {
        if (!isNetWorkConnect()) {
            return;
        } else {
            showLoadingView();
            Map<String, Object> reqParam = UserInfoManager.getUserKeyMapIfLogin();
            HttpRequestUtils.post(false, UrlConstants.FUNCTION_MESAGE_TYPE_LIST, reqParam, false, new HttpCallBack() {
                @Override
                public void onSucced(String data) {
                    hideLoadingView();
                    datas.clear();
                    swipe_container.setRefreshing(false);
                    List<MessageType> temData = FastjsonUtils.getBeanList(data, MessageType.class);
//                    MessageType test1 = new MessageType();
//                    test1.setClass_id(5);
//                    test1.setClass_name("而是测试");
//                    test1.setDuration_gold(345);
//                    List<MessageTypeChildBean> childs = new ArrayList<MessageTypeChildBean>();
//                    MessageTypeChildBean childBean = new MessageTypeChildBean();
//                    childBean.setClass_id(34);
//                    childBean.setClass_name("zizi");
//                    childBean.setOnce_gold(43);
//                    MessageTypeChildBean childBean2 = new MessageTypeChildBean();
//                    childBean2.setClass_id(341);
//                    childBean2.setClass_name("zi2zi");
//                    childBean2.setOnce_gold(423);
//                    MessageTypeChildBean childBean3 = new MessageTypeChildBean();
//                    childBean3.setClass_id(3421);
//                    childBean3.setClass_name("xcxcvci");
//                    childBean3.setOnce_gold(43);
//                    childs.add(childBean);
//                    childs.add(childBean2);
//                    childs.add(childBean3);
//                    test1.setChild(childs);
//                    temData.add(test1);
//                /*test*/
//                    MessageType test2 = new MessageType();
//                    test2.setClass_id(5);
//                    test2.setClass_name("而是测试");
//                    test2.setDuration_gold(345);
//                    List<MessageTypeChildBean> childs11 = new ArrayList<MessageTypeChildBean>();
//                    MessageTypeChildBean childBean11 = new MessageTypeChildBean();
//                    childBean11.setClass_id(34);
//                    childBean11.setClass_name("zizi");
//                    childBean11.setOnce_gold(43);
//                    MessageTypeChildBean childBean12 = new MessageTypeChildBean();
//                    childBean12.setClass_id(341);
//                    childBean12.setClass_name("zi2zi");
//                    childBean12.setOnce_gold(423);
//                    MessageTypeChildBean childBean13 = new MessageTypeChildBean();
//                    childBean13.setClass_id(3421);
//                    childBean13.setClass_name("xcxcvci");
//                    childBean13.setOnce_gold(43);
//                    MessageTypeChildBean childBean14 = new MessageTypeChildBean();
//                    childBean14.setClass_id(3421);
//                    childBean14.setClass_name("xcxcvci");
//                    childBean14.setOnce_gold(43);
//                    childs11.add(childBean11);
//                    childs11.add(childBean12);
//                    childs11.add(childBean13);
//                    childs11.add(childBean14);
//                    test2.setChild(childs11);
//                    temData.add(test2);
                    setData(temData);
                }

                @Override
                public void onBadHttp(String tip) {
                    hideLoadingView();
                    swipe_container.setRefreshing(false);
                    //showDefaultView();
                }

                @Override
                public void onErrorCode(NewWorkBean bean) {
                    hideLoadingView();
                    swipe_container.setRefreshing(false);
                    // showDefaultView();
                }
            });
        }
    }

    private void setData(List<MessageType> temData) {
        if (temData != null && temData.size() > 0) {
            for (int i = 0; i < temData.size(); i++) {
                MessageType item = temData.get(i);
                List<MessageTypeChildBean> childs = temData.get(i).getChild();
                for (int j = 0; j < childs.size(); j++) {
                    item.addSubItem(childs.get(j));
                }
                datas.add(item);
            }
            mQuickAdapter.notifyDataSetChanged();
        } else {
            //
        }
    }

    /**
     * 弹出菜单
     */
    private void showPopupWindows(final MessageType itemParent, final int position) {
        if (itemParent.getSubscribe() == 1) {  //退订
            showConfrimDialog("退订后不再收到相关推送，并且不会返回订阅所消耗的金币，确定退订吗？", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    handleParentMessageClick(itemParent, position);
                }
            });

        } else {//订购
            new PopupWindows(mContext, getString(R.string.buy_bigmessge_confirm) + "(" + itemParent.getDuration() + "天)", itemParent.getDuration_gold(), mRecyclerView, new PopupWindows.PopOnClickListener() {
                @Override
                public void onClickYes() {
                    handleParentMessageClick(itemParent, position);
                }

                @Override
                public void onClickNo() {

                }
            });
        }

    }

    private void handleParentMessageClick(MessageType itemParent, final int position) {
        if (isNetWorkConnect()) {
            showLoadingView();
            Map<String, Object> reqParam = UserInfoManager.getUserKeyMapIfLogin();
            reqParam.put("class_id", itemParent.getClass_id());
            if (itemParent.getSubscribe() == 1) {
                reqParam.put("action", "cancel");
            } else {
                reqParam.put("action", "subscribe");
            }

            HttpRequestUtils.post(false, UrlConstants.FUNCTION_BUY_MESSAGE, reqParam, false, new HttpCallBack() {
                @Override
                public void onSucced(String data) {
                    hideLoadingView();
                    int result = FastjsonUtils.getJsonInteger(data, "subscribe");
                    if (result == 1) {
                        showShortToast("订阅成功");
                    } else {
                        showShortToast("退订成功");
                    }
                    MessageType changedItem = (MessageType) datas.get(position);
                    changedItem.setSubscribe(result);
                    if (changedItem.getChild() != null && changedItem.getChild().size() > 0) {
                        for (MessageTypeChildBean itemChild : changedItem.getChild()) {
                            itemChild.setSubscribe(result);
                        }
                    }
                    mQuickAdapter.notifyDataSetChanged();
                }

                @Override
                public void onBadHttp(String tip) {
                    hideLoadingView();
                    showShortToast(tip);
                }

                @Override
                public void onErrorCode(NewWorkBean bean) {
                    hideLoadingView();
                    if (bean.getCode() == 106) {
                        showShortToast("金币不足，请先充值");
                        startActivity(RechargeGlodActivity.class);
                    } else {
                        showShortToast(bean.getMsg());
                    }
                }
            });
        }

    }

    private void handleChlidMessageClick(final MessageTypeChildBean itemChild) {
        if (isNetWorkConnect()) {
            showLoadingView();
            Map<String, Object> reqParam = UserInfoManager.getUserKeyMapIfLogin();
            reqParam.put("class_id", itemChild.getClass_id());
            if (itemChild.getSubscribe() == 1) {
                reqParam.put("action", "cancel");
            } else {
                reqParam.put("action", "subscribe");
            }

            HttpRequestUtils.post(false, UrlConstants.FUNCTION_BUY_MESSAGE, reqParam, false, new HttpCallBack() {
                @Override
                public void onSucced(String data) {
                    hideLoadingView();
                    int result = FastjsonUtils.getJsonInteger(data, "subscribe");
                    if (result == 1) {
                        showShortToast("订阅成功");
                    } else {
                        showShortToast("退订成功");
                    }

                    itemChild.setSubscribe(result);
                    mQuickAdapter.notifyDataSetChanged();
                }

                @Override
                public void onBadHttp(String tip) {
                    hideLoadingView();
                    showShortToast(tip);
                }

                @Override
                public void onErrorCode(NewWorkBean bean) {
                    hideLoadingView();
                    showShortToast(bean.getMsg());
                }
            });
        }

    }

    @Override
    public void onClickParent(MessageType itemParent, final int position) {
        if (NotificationsUtils.isNotificationEnabled(this)) {
            showPopupWindows(itemParent, position);
        } else {
            showConfrimDialog("为了收到订阅消息推送，请您开启通知栏权限", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Intent intent = new Intent(Settings.ACTION_APN_SETTINGS);
                    startActivity(intent);
                }
            });
        }
    }

    @Override
    public void onClickChilde(final MessageTypeChildBean itemChild, int pPosition, int cPosition) {
        if (NotificationsUtils.isNotificationEnabled(this)) {
            handleChlidMessageClick(itemChild);
        } else {
            showConfrimDialog("为了收到订阅消息推送，请您开启通知栏权限", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Intent intent = new Intent(Settings.ACTION_SETTINGS);
                    startActivity(intent);
                }
            });
        }
    }


}
